<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="160px"
      v-loading="formLoading"
    >
      <el-form-item label="邮箱" prop="mail">
        <el-input v-model="formData.mail" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item label="SMTP 服务器域名" prop="host">
        <el-input v-model="formData.host" placeholder="请输入SMTP 服务器域名" />
      </el-form-item>
      <el-form-item label="SMTP 服务器端口" prop="port">
        <el-input-number v-model="formData.port" placeholder="请输入SMTP 服务器端口" />
      </el-form-item>
      <el-form-item label="是否开启 SSL" prop="sslEnable">
        <el-radio-group v-model="formData.sslEnable">
          <el-radio v-for="dict in common_bool_type" :key="String(dict.value)" :value="dict.value">
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否开启 STARTTLS" prop="starttlsEnable">
        <el-radio-group v-model="formData.starttlsEnable">
          <el-radio v-for="dict in common_bool_type" :key="String(dict.value)" :value="dict.value">
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!--      <el-form-item label="文件上传" prop="testFile">-->
      <!--        <UploadFile v-model="formData.testFile" :limit="1" :is-show-tip="false" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="图片上传" prop="testImg">-->
      <!--        <UploadImg v-model="formData.testImg">-->
      <!--          <template #tip>建议宽度 750px</template>-->
      <!--        </UploadImg>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="多图片上传" prop="testImgs">-->
      <!--        <UploadImgs v-model="formData.testImgs">-->
      <!--          <template #tip>建议宽度 750px</template>-->
      <!--        </UploadImgs>-->
      <!--      </el-form-item>-->
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { MailAccountApi, MailAccountVO } from '@/api/system/mail/mail_account'
import { FormRules } from 'element-plus'

defineOptions({ name: 'SystemMailAccountForm' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const { common_bool_type } = useBoolDict(DICT_TYPE.common_bool_type)

const dialogVisible = ref(false) // dialog是否展示
const dialogTitle = ref('') // dialog标题
const formLoading = ref(false) // 表单加载中标记，1. 修改时的数据加载，2. 提交的按钮禁用
const formType = ref('') // 表单类型，新增：create，修改：update
const formDataObj = {
  id: undefined,
  mail: undefined,
  username: undefined,
  password: undefined,
  host: undefined,
  port: 465,
  sslEnable: undefined,
  starttlsEnable: undefined
  // testFile: undefined,
  // testImg: undefined,
  // testImgs: undefined
}
const formData = ref({ ...formDataObj })
const formRules = reactive<FormRules>({
  mail: [{ required: true, message: '邮箱不能为空', trigger: 'blur' }],
  username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
  password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
  host: [{ required: true, message: 'SMTP 服务器域名不能为空', trigger: 'blur' }],
  port: [{ required: true, message: 'SMTP 服务器端口不能为空', trigger: 'blur' }],
  sslEnable: [{ required: true, message: '是否开启 SSL不能为空', trigger: 'blur' }],
  starttlsEnable: [{ required: true, message: '是否开启 STARTTLS不能为空', trigger: 'blur' }]
  // testFile: [{ required: true, message: '文件不能为空', trigger: 'blur' }],
  // testImg: [{ required: true, message: '图片不能为空', trigger: 'blur' }],
  // testImgs: [{ required: true, message: '图片不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MailAccountApi.getMailAccount(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const emit = defineEmits(['success']) // 定义success事件，用于操作成功后的回调
const submitForm = async () => {
  if (await formValidate(formRef)) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MailAccountVO
    if (formType.value === 'create') {
      await MailAccountApi.createMailAccount(data)
      message.success(t('common.createSuccess'))
    } else {
      await MailAccountApi.updateMailAccount(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = { ...formDataObj }
  formRef.value?.resetFields()
}
</script>

<style scoped lang="scss"></style>
